<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="艺术品名字" prop="name">
      <el-input v-model="dataForm.name" placeholder="艺术品名字"></el-input>
    </el-form-item>
    <el-form-item label="艺术品介绍" prop="description">
      <el-input v-model="dataForm.description" placeholder="艺术品介绍"></el-input>
    </el-form-item>
    <el-form-item label="艺术品图片" prop="image">
      <el-input v-model="dataForm.image" placeholder="艺术品图片"></el-input>
    </el-form-item>
    <el-form-item label="艺术品作者" prop="author">
      <el-input v-model="dataForm.author" placeholder="艺术品作者"></el-input>
    </el-form-item>
    <el-form-item label="艺术馆id" prop="museum">
      <el-input v-model="dataForm.museum" placeholder="艺术馆id"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="价格"></el-input>
    </el-form-item>
    <el-form-item label="作品风格" prop="style">
      <el-input v-model="dataForm.style" placeholder="作品风格"></el-input>
    </el-form-item>
    <el-form-item label="作品销量" prop="saleCount">
      <el-input v-model="dataForm.saleCount" placeholder="作品销量"></el-input>
    </el-form-item>
    <el-form-item label="艺术品库存" prop="ware">
      <el-input v-model="dataForm.ware" placeholder="艺术品库存"></el-input>
    </el-form-item>
    <el-form-item label="创建日期" prop="gmtCreate">
      <el-date-picker
      v-model="dataForm.gmtCreate"
      type="datetime"
      placeholder="创建日期">
    </el-date-picker>
    </el-form-item>
    <el-form-item label="修改日期" prop="gmtModified">
       <el-date-picker
      v-model="dataForm.gmtModified"
      type="datetime"
      placeholder="修改日期">
    </el-date-picker>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          description: '',
          image: '',
          author: '',
          museum: '',
          price: '',
          style: '',
          saleCount: '',
          ware: '',
          gmtCreate: '',
          gmtModified: ''
        },
        dataRule: {
          name: [
            { required: true, message: '艺术品名字不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '艺术品介绍不能为空', trigger: 'blur' }
          ],
          image: [
            { required: true, message: '艺术品图片不能为空', trigger: 'blur' }
          ],
          author: [
            { required: true, message: '艺术品作者不能为空', trigger: 'blur' }
          ],
          museum: [
            { required: true, message: '艺术馆id不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          style: [
            { required: true, message: '作品风格不能为空', trigger: 'blur' }
          ],
          saleCount: [
            { required: true, message: '作品销量不能为空', trigger: 'blur' }
          ],
          ware: [
            { required: true, message: '艺术品库存不能为空', trigger: 'blur' }
          ],
          gmtCreate: [
            { required: true, message: '创建日期不能为空', trigger: 'blur' }
          ],
          gmtModified: [
            { required: true, message: '修改日期不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/cloudgallery/product/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.product.name
                this.dataForm.description = data.product.description
                this.dataForm.image = data.product.image
                this.dataForm.author = data.product.author
                this.dataForm.museum = data.product.museum
                this.dataForm.price = data.product.price
                this.dataForm.style = data.product.style
                this.dataForm.saleCount = data.product.saleCount
                this.dataForm.ware = data.product.ware
                this.dataForm.gmtCreate = data.product.gmtCreate
                this.dataForm.gmtModified = data.product.gmtModified
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cloudgallery/product/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'description': this.dataForm.description,
                'image': this.dataForm.image,
                'author': this.dataForm.author,
                'museum': this.dataForm.museum,
                'price': this.dataForm.price,
                'style': this.dataForm.style,
                'saleCount': this.dataForm.saleCount,
                'ware': this.dataForm.ware,
                'gmtCreate': this.dataForm.gmtCreate,
                'gmtModified': this.dataForm.gmtModified
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
